package com.hunttown.mes.impl;

import com.hunttown.mes.rpc.domain.query.AnalysisIpDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisIpRpcService;
import com.hunttown.mes.rpc.domain.AnalysisIpDTO;
import com.hunttown.mes.service.AnalysisIpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisIpConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:04:59
 */
@Service
public class AnalysisIpRpcServiceImpl implements AnalysisIpRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisIpRpcServiceImpl.class);

    private final AnalysisIpService analysisIpService;

    @Autowired
    public AnalysisIpRpcServiceImpl(AnalysisIpService analysisIpService) {
        this.analysisIpService = analysisIpService;
    }

    @Override
    public AnalysisIpDTO insert(AnalysisIpDTO analysisIpDTO) {
        try {
            return convert(analysisIpService.insert(convert(analysisIpDTO)));
        } catch (Exception e) {
            logger.error("AnalysisIp create failed.", e);
            throw new RuntimeException("AnalysisIp create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisIpDTO analysisIpDTO) {
        try {
            return analysisIpService.updateInfoById(convert(analysisIpDTO));
        } catch (Exception e) {
            logger.error("AnalysisIp update by id failed.", e);
            throw new RuntimeException("AnalysisIp update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisIpDTOQuery analysisIpDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisIpDTOQuery.getW_id() != null && analysisIpDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisIpDTOQuery.getW_id());
            }

            if (analysisIpDTOQuery.getIds() != null && !analysisIpDTOQuery.getIds().equals("")) {
                query.put("ids", analysisIpDTOQuery.getIds());
            }

            return analysisIpService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisIp update by query failed.", e);
            throw new RuntimeException("AnalysisIp update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisIpDTO selectById(Integer id) {
        try {
            return convert(analysisIpService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisIp select by id failed.", e);
            throw new RuntimeException("AnalysisIp select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisIpDTO selectObjByQuery(AnalysisIpDTOQuery analysisIpDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisIpService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisIp select obj by query failed.", e);
            throw new RuntimeException("AnalysisIp select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisIpDTO> getListForPage(AnalysisIpDTOQuery analysisIpDTOQuery) {
        Query query = Query.page(analysisIpDTOQuery.getPn(), analysisIpDTOQuery.getPageSize());

        if (analysisIpDTOQuery.getIds() != null && !analysisIpDTOQuery.getIds().equals("")) {
            query.put("ids", analysisIpDTOQuery.getIds());
        }

        if (analysisIpDTOQuery.getLikeName() != null && !analysisIpDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisIpDTOQuery.getLikeName());
        }

        if (analysisIpDTOQuery.getOrderBy() != null && analysisIpDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisIpDTOQuery.getOrderBy());
        }

        List<AnalysisIpDTO> list = convert(analysisIpService.selectByQuery(query));
        Page<AnalysisIpDTO> page = Page.from(analysisIpDTOQuery.getPn(), analysisIpDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisIpService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisIpService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisIp delete failed.", e);
            throw new RuntimeException("AnalysisIp delete failed, Please order your code.", e);
        }
    }
}